﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class webadmin_pages_cp_login : System.Web.UI.Page
{
    private Common comm = new Common();
    private string mat_khau;
    private DataTable menuMain;
    public string mess = "Đăng Nhập hệ thống quản trị";
    private string tai_khoan;
    public string url = "";

    protected void _btnok_ServerClick1(object sender, EventArgs e)
    {
        string mess = this.mess;
        this.mess = mess + "" + this._name.Value + "|" + this._pass.Value;
        this.tai_khoan = this.comm.TextFormat(this._name.Value.Trim());
        this.mat_khau = this.comm.TextFormat(this._pass.Value.Trim());
        this.menuMain = this.comm.login(this.tai_khoan, this.mat_khau);
        if (this.menuMain.Rows.Count > 0)
        {
            this.Session.Add("UserID", this.menuMain.Rows[0][0]);
            this.Session.Add("tai_khoan", this.menuMain.Rows[0]["tai_khoan"]);
            this.Session.Add("ten_hien_thi", this.menuMain.Rows[0]["ten_hien_thi"]);
            this.Session.Add("ten_nhom_nd", this.menuMain.Rows[0]["ten_nhom_nd"]);
            this.Session.Add("mo_ta", this.menuMain.Rows[0]["mo_ta"]);
            this.Session.Add("menuMain", this.menuMain);
            DataSet _ds = this.comm.ExeceuteDataset("select * from PhanQuyen_nguoi_dung where tai_khoan = '"+this.menuMain.Rows[0]["tai_khoan"].ToString()+"'");
            
            if(_ds.Tables[0].Rows.Count > 0)
            {
				this.Session.Add("ma_nhom_nd", _ds.Tables[0].Rows[0]["ma_nhom_nd"].ToString());
            }
            if (!string.IsNullOrEmpty(this.url))
            {
                base.Response.Redirect(this.url);
            }
            else
            {
                base.Response.Redirect("Default.aspx");
            }
        }
        else
        {
            this.mess = "Đăng nhập thất bại. C\x00f3 thể kh\x00f4ng đ\x00fang t\x00e0i khoản hoặc mật khẩu sai.";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["url"] != null)
        {
            this.url = this.Session["url"].ToString();
        }
        if (this.Session["UserID"] != null)
        {
            this.Session.Clear();
        }
    }
}
